import { v4 as uuidv4 } from "uuid";

// 添加一个路由为layout的子路由
export const addRoute = (id, route) => {
  console.log({ id, route });
};
// 查询是否存在相同ID
export const ifrepeatId = (array = [], id) => {
  return array.map((item) => item.meta.id).includes(id);
};
// 查询是否存在相同标题（title）
export const ifrepeatTitle = (array = [], parentid = null, title = "") => {
  return array
    .filter((i) => i.meta.pid == parentid)
    .map((item) => item.meta.title)
    .includes(title);
};
// 查询是否存在相同的英文名称（name)
export const ifrepeatName = (array = [], name = "") => {
  return array.map((item) => item.meta.title).includes(name);
};
// 判断是否是一个菜单组
export const ifMenuBar = (data) => {
  let ifMenuBar = data?.meta?.display?.ifMenuBar ?? false;
  return ifMenuBar;
};

// 格式化数据
export const formatMenuItem = (item) => {
  let { name = "", path = "", redirect = null, component = null } = item;
  let meta = item.meta;
  let display = meta?.display ?? {};
  let componentFilePath = meta.component
    ? `@/pages/${meta.component}.vue`
    : null;
  return {
    name,
    path,
    redirect,
    component,
    meta: {
      id: meta.id,
      pid: meta.pid ?? null,
      title: meta.title ?? "",
      icon: meta.icon ?? "",
      component: meta.component,
      componentFilePath,
      storey: 0, // 菜单层级
      display: {
        // 显示
        ifMenuBar: display?.ifMenuBar ?? false, // 是否为菜单
        breadcrumb: display?.breadcrumb ?? true, // 面包屑显示
        navigator: display?.navigator ?? true, // 导航菜单显示
        navigatorRightIcon: display?.navigatorRightIcon ?? false, // 菜单右侧箭头图标
        normalIconClass: meta.icon ?? "", // 正常状态菜单图标
        hoverIconClass: meta.icon ?? "", // hover状态菜单图标
        activedChangeId: null, // 当菜单激活时,将激活的ID指向这个ID，用于高亮其他ID的菜单
        activedParentHight: false, // 当这个菜单激活时，是否将父菜单组高亮
        childrenDevelop: display?.childrenDevelop ?? false,
      },
      camouflage: {
        // 包装
        type: "",
        tag: "",
        href: "",
        target: "_blank",
      },
      skip: {
        // 页面跳转
        type: "push", // 向路由表示，当路由跳转时以那种方式进行跳转
      },
      page: {
        // 页面配置
      },
      keepAlive: {
        // 缓存
      },
      sort: {
        // 排序
        top: false, // 置顶
        index: null, // 排序Index
      },
    },
  };
};

// 新增主菜单
export const addMenuForm = (
  options = {
    pid: null,
    title: "",
    name: "",
    path: "",
  }
) => {
  let defualtConfig = {
    icon: "icon-zhuti1",
    pid: null,
    title: "",
    name: "",
    path: "",
  };
  // uuid生成唯一ID
  let id = uuidv4();
  let pid = options.meta.pid ?? defualtConfig.pid;
  let title = options.meta.title ?? defualtConfig.title;
  let name = options.name;
  let path = options.path;
  let icon = options.icon ?? defualtConfig.icon;
  return formatMenuItem({
    name,
    path,
    meta: {
      id,
      pid,
      title,
      icon,
      display: {
        ifMenuBar: true,
      },
    },
  });
};
export default {
  ifrepeatId,
  ifrepeatTitle,
  ifrepeatName,
  ifMenuBar,
  addMenuForm,
  formatMenuItem,
};
